from flask import Blueprint, jsonify, current_app
from demo1.utils.scheduler_utils import reload_jobs

reload_bp = Blueprint('reload', __name__)


# 页面上触发重载任务列表的接口
@reload_bp.route('/api/reload-jobs', methods=['GET','POST'])
def reload_jobs_api():
    try:
        # 从 app 上下文中获取 scheduler
        scheduler = current_app.extensions.get('scheduler')
        if not scheduler:
            return jsonify({'status': 'error', 'message': 'Scheduler not initialized'}), 500
        reload_jobs(scheduler)

        return jsonify({'status': 'success', 'message': 'Jobs reloaded successfully'})
    except Exception as e:
        return jsonify({'status': 'error', 'message': str(e)}), 500